/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import dao.Connector;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class StatisticController {
    private dao.Statistic stDB = new dao.Statistic();
    
    // Phuong thuc lay ra danh sach thong ke theo thang, NOTE : month = {0,...,11}.
    
    public ArrayList<entity.Statistic> getByMonth(int month, int year){
        ArrayList<entity.Statistic> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        
        cal.set(year, month, 1);
        Date firstDate = cal.getTime();
        cal.set(year, month%12 + 1, 1);
        Date lastDate = cal.getTime();
        
        if(lastDate.getTime() > new Date().getTime()){
            lastDate = new Date();
        }
        
        long h = 24*3600*1000;
        for(Date date = firstDate; date.getTime() < lastDate.getTime(); date = new Date(date.getTime() +h)){
            entity.Statistic stt = new entity.Statistic();
            java.sql.Date dateTemp = new java.sql.Date(date.getTime());
            SimpleDateFormat sdf =  new SimpleDateFormat("dd - MM - yyyy");
            
            stt.setDate(sdf.format(date));
            stt.setBookingN(stDB.sumBookingByDate(dateTemp));
            stt.setCheckoutN(stDB.sumCheckoutByDate(dateTemp));
            stt.setRevenue(stDB.sumRevenueByDate(dateTemp));
            
            if((stt.getBookingN() != 0)||(stt.getCheckoutN()!=0)){
                list.add(stt);
            }
        }
        return list;
    }


   
}
